<?php
/**
 * Created by PhpStorm.
 * User: wk
 * Date: 2018/4/12
 * Time: 21:37
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Request;
use think\Response;
use think\Session;
class Login extends Controller
{
    public function index()
    {
        return view('index/login');
    }
    /**
     * 后台登录处理
     * @return \think\response\View
     */
    public function dologin(Request $request)
    {
//        var_dump($request);die;
        $data=$request->post();
//            var_dump($data);


        $pwd=hash('md5',$data['userpass']);
        $info['username']=$data['username'];
        $info['userpass']=$pwd;

        $res=Db::table('user')
            ->field(['id','username'])
            ->where($info)
            ->find();
//        var_dump($res);die;
        if ($res){
//            echo '123';die;
            // 存session
            Session::set('admin.username',$res['username']);
            Session::set('admin.id',$res['id']);

            //登录成功过后,将用户信息作为条件查询该用户所拥有的权限
            $nodelist=Db::table('user u,user_role ur,role r,role_node rn,node n')
                    ->field('mname,group_concat(aname) anames')
                    ->where('u.id=ur.uid and r.id=ur.rid and r.id=rn.rid and n.id=rn.nid')
                    ->where('r.status=1')
                    ->where('n.status=1')
                    ->where('uid','=',$res['id'])
                    ->group('mname')
                    ->select();

//            var_dump($nodelist);die;
           Session::set('nodelist',$nodelist);
//            $data=Session::get('admin');
//            var_dump($data);die();
            $this->success('登录成功','admin/Main/index');
        }else{
            $this->success('登录失败');
        }



    }

    /**
     * 后台登出处理
     * @return \think\response\View
     */
    public function logout()
    {
        $this->redirect('admin/index/index');
    }
}